package com.ruoyi.project.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysQuery;
import com.ruoyi.project.system.domain.TaxInfo;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Comparator;

public class SmHandler {

    public static AjaxResult queryDataHandler(AjaxResult queryData, SysQuery smQuery, String endTime) {
        if (StringUtils.isNotEmpty(smQuery.getName())) {
            queryData.put("name", smQuery.getName());
        }
        if (StringUtils.isNotEmpty(smQuery.getCardNo())) {
            queryData.put("cardNo", "******" + smQuery.getCardNo().substring(6));
        }
        if (StringUtils.isNotEmpty(smQuery.getMobile())) {
            queryData.put("tel", "*******" + smQuery.getMobile().substring(7));
        }
        queryData.put("endTime", endTime);
        return queryData;
    }

    public static JSONObject handleTaxes(TaxInfo taxInfo) {
        JSONObject data = new JSONObject();

        data.put("taxId", taxInfo.getTaxId());
        data.put("startMonth", taxInfo.getStartMonth());
        data.put("companyName", taxInfo.getCompanyName());
        data.put("evaluationResult", taxInfo.getEvaluationResult());
        data.put("taxCollectEndTime", taxInfo.getTaxCollectEndTime());
        data.put("score", taxInfo.getScore());
        data.put("rank", taxInfo.getRank());
        data.put("reportNo", taxInfo.getReportNo());
        data.put("endMonth", taxInfo.getEndMonth());
        data.put("lastCorporateChange", taxInfo.getLastCorporateChange());
        data.put("queryTime", taxInfo.getQueryTime());
        data.put("creditlimit", taxInfo.getCreditlimit());

        data.put("enterprise", StringUtils.isNotEmpty(taxInfo.getEnterprise()) ? JSON.parse(taxInfo.getEnterprise()) : "");
        data.put("trademark", StringUtils.isNotEmpty(taxInfo.getTrademark()) ? JSON.parse(taxInfo.getTrademark()) : "");
        data.put("patent", StringUtils.isNotEmpty(taxInfo.getPatent()) ? JSON.parse(taxInfo.getPatent()) : "");
        data.put("copyright", StringUtils.isNotEmpty(taxInfo.getCopyright()) ? JSON.parse(taxInfo.getCopyright()) : "");
        data.put("copyrightSoft", StringUtils.isNotEmpty(taxInfo.getCopyrightSoft()) ? JSON.parse(taxInfo.getCopyrightSoft()) : "");
        data.put("baseinfoVo", StringUtils.isNotEmpty(taxInfo.getBaseinfoVo()) ? JSON.parse(taxInfo.getBaseinfoVo()) : "");


        data.put("coreTaxFinanceIndex", StringUtils.isNotEmpty(taxInfo.getCoreTaxFinanceIndex()) ? JSON.parse(taxInfo.getCoreTaxFinanceIndex()) : "");

        data.put("supplierList", StringUtils.isNotEmpty(taxInfo.getSupplierList()) ? JSON.parse(taxInfo.getSupplierList()) : "");
        data.put("wenshu", StringUtils.isNotEmpty(taxInfo.getWenshu()) ? JSON.parse(taxInfo.getWenshu()) : "");
        data.put("ndQyfpxyjxsList", StringUtils.isNotEmpty(taxInfo.getNdQyfpxyjxsList()) ? JSON.parse(taxInfo.getNdQyfpxyjxsList()) : "");
        data.put("ndQyfpxycpList", StringUtils.isNotEmpty(taxInfo.getNdQyfpxycpList()) ? JSON.parse(taxInfo.getNdQyfpxycpList()) : "");
        data.put("yshdInfos", StringUtils.isNotEmpty(taxInfo.getYshdInfos()) ? JSON.parse(taxInfo.getYshdInfos()) : "");
        data.put("disrupt", StringUtils.isNotEmpty(taxInfo.getDisrupt()) ? JSON.parse(taxInfo.getDisrupt()) : "");
        data.put("undertaker", StringUtils.isNotEmpty(taxInfo.getUndertaker()) ? JSON.parse(taxInfo.getUndertaker()) : "");
        data.put("courtInfo", StringUtils.isNotEmpty(taxInfo.getCourtInfo()) ? JSON.parse(taxInfo.getCourtInfo()) : "");
        data.put("enterpriseInfoPrompt", comItemHandler(taxInfo.getEnterpriseInfoPrompt()));
        data.put("Inspection", comItemHandler(taxInfo.getInspection()));

        JSONArray array = JSON.parseArray(taxInfo.getTaxDeclareSjjeByYear());
        array.sort(Comparator.comparing(obj -> ((JSONObject) obj).getInteger("year")).reversed());
        int size = array.size();
        JSONObject json1 = size > 0 ? array.getJSONObject(0) : null;
        JSONObject json2 = size > 1 ? array.getJSONObject(1) : null;
        JSONObject json3 = size > 2 ? array.getJSONObject(2) : null;

        JSONArray kpsort = new JSONArray();

        JSONObject i_ = new JSONObject();
        i_.put("time", "月份");
        i_.put("fm", json1 != null ? json1.getInteger("year") : "-");
        i_.put("sm", json2 != null ? json2.getInteger("year") : "-");
        i_.put("tm", json3 != null ? json3.getInteger("year") : "-");
        kpsort.add(i_);

        for (int i = 0; i < 12; i++) {
            JSONObject item = new JSONObject();
            item.put("time", (i + 1) + "月");
            item.put("fm", json1 != null ? json1.getJSONArray("taxpayAmount").get(i) : "-");
            item.put("sm", json2 != null ? json2.getJSONArray("taxpayAmount").get(i) : "-");
            item.put("tm", json3 != null ? json3.getJSONArray("taxpayAmount").get(i) : "-");
            kpsort.add(item);
        }
        JSONObject item = new JSONObject();
        item.put("time", "年度汇总");
        item.put("fm", json1 != null ? json1.getFloatValue("total") : "-");
        item.put("sm", json2 != null ? json2.getFloatValue("total") : "-");
        item.put("tm", json3 != null ? json3.getFloatValue("total") : "-");
        kpsort.add(item);
        data.put("taxDeclareSjjeByYear", kpsort);

        JSONArray array_qybp = JSON.parseArray(taxInfo.getQyfpxxList());
        array_qybp.sort(Comparator.comparing(obj -> ((JSONObject) obj).getInteger("nd")).reversed());
        data.put("qyfpxxList", array_qybp);
        return data;
    }

    public static StringBuilder readLine() {
        String filePath = "C:\\Users\\Administrator\\Desktop\\COM.txt";
        // 使用 StringBuilder 存储读取到的文件内容
        StringBuilder content = new StringBuilder();
        // 使用 try-with-resources 自动关闭 BufferedReader
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            // 按行读取文本文件，直到文件末尾
            while ((line = reader.readLine()) != null) {
                content.append(line);
//                content.append(System.lineSeparator()); // 添加换行符，保持原始格式
            }
        } catch (IOException e) {
            System.err.println("读取文件时发生错误: " + e.getMessage());
            e.printStackTrace();
            return null;
        }
        return content;
    }

    public static Object comItemHandler(String ss) {
        return StringUtils.isNotEmpty(ss) ? JSON.parse(ss) : "";
    }

}
